package com.lcy.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lcy.app.R;
import com.lcy.app.activity.child.ChildFileAdapter;
import com.lcy.app.api.MusicApi;
import com.lcy.app.api.domain.BusinessFileDTO;
import com.lcy.app.api.domain.RespEntity;
import com.lcy.app.api.domain.SysBusinessFileDTO;
import com.lcy.app.api.domain.SysDir;
import com.lcy.app.api.http.HTTP;
import com.lcy.app.common.util.HttpUtil;
import com.lcy.app.common.util.StrUtil;
import com.lcy.app.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DiskActivity extends AppCompatActivity {
    private LinearLayout currentDirContainer;
    private LinearLayout dirContainer;
    private ListView fileContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disk);

//        horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
        dirContainer = findViewById(R.id.dirList);
        fileContainer = findViewById(R.id.fileList);
        currentDirContainer = findViewById(R.id.currentDirContainer);

        showDomain("-1");
    }

    private void showDomain(String dirId) {
        HTTP<BusinessFileDTO> businessStraightFileDTOHTTP = new HTTP<>();
        RespEntity<BusinessFileDTO> musicResEntity = businessStraightFileDTOHTTP
                .synRequest(this, retrofit ->
                        retrofit.create(MusicApi.class).findSelfDomain(MainActivity.token, dirId));

        if (HttpUtil.isOk(musicResEntity)) {
            BusinessFileDTO straightFileDTO = musicResEntity.getData();

            List<SysBusinessFileDTO> businessFileList = straightFileDTO.getBusinessFileList();
            List<SysDir> straightDirList = straightFileDTO.getStraightDirList();
            List<SysDir> currentDirList = straightFileDTO.getCurrentDirList();

            bindDirData(currentDirContainer,currentDirList, "/");
            listItem(straightDirList, businessFileList);
        }
    }

    public void listItem(List<SysDir> straightDirList, List<SysBusinessFileDTO> businessFileList) {
        // 处理文件夹
        bindDirData(dirContainer,straightDirList, "");

        // 处理文件
        bindFileData(businessFileList);
    }

    private void bindDirData(LinearLayout linearLayout,List<SysDir> straightDirList, String endStr) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.setMargins(20, 10, 20, 10);

        linearLayout.removeAllViews();
        for (int i = 0; i < straightDirList.size(); i++) {
            TextView textView = new TextView(this);
            textView.setText(straightDirList.get(i).getName() + endStr);
            textView.setTextColor(Color.BLACK);
            textView.setLayoutParams(layoutParams);
            int k = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDomain(straightDirList.get(k).getId());
                }
            });

            linearLayout.addView(textView);
            linearLayout.invalidate();
        }
    }

    private void bindFileData(List<SysBusinessFileDTO> businessFileList) {
        // 创建Adapter
        ChildFileAdapter adapter = new ChildFileAdapter(this, R.layout.activity_child_file, businessFileList);

        // 为ListView绑定Adapter
        fileContainer.setAdapter(adapter);
        // 监听器
        fileContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }
}
